/*
 * SystemService.java
 * Copyright (c) 2014, 2019, Kiri. 
 * All rights reserved.
 */
package com.onesalt.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.onesalt.domain.DinnerCancelRule;
import com.onesalt.domain.system.SysParam;
import com.onesalt.utils.JSONUtil;

/**
 * 
 *
 * @author kiri
 * @date 2015年4月19日
 * @version 1.0
 */
@Service
public class SystemService extends AbstractService {

	private final SysParamRepository sysParamRepository;

	private final DinnerCancelRuleRepository dinnerCancelRuleRepository;

	public static final String DINNER_CANCEL_RULES_KEY = "client.dinner_cancel_rules";

	@Autowired
	public SystemService(SysParamRepository sysParamRepository, DinnerCancelRuleRepository dinnerCancelRuleRepository) {
		this.sysParamRepository = sysParamRepository;
		this.dinnerCancelRuleRepository = dinnerCancelRuleRepository;
	}

	public SysParamRepository getSysParamRepository() {
		return sysParamRepository;
	}

	public DinnerCancelRuleRepository getDinnerCancelRuleRepository() {
		return dinnerCancelRuleRepository;
	}

	public Map<String, String> getAllClientParams() {
		List<SysParam> paramList = getSysParamRepository().findByHidden(false);
		Map<String, String> map = new HashMap<String, String>();
		for (SysParam sysParam : paramList) {
			map.put(sysParam.getCode(), sysParam.getValue());
		}
		List<DinnerCancelRule> dcRules = getDinnerCancelRuleRepository().findByValidOrderBySortAsc(true);
		if (!CollectionUtils.isEmpty(dcRules)) {
			getLogger().debug("obj = " + dcRules);
			getLogger().debug("dcRules string = " + JSONUtil.toJsonString(dcRules));
			map.put(DINNER_CANCEL_RULES_KEY, JSONUtil.toJsonString(dcRules));
		}
		return map;
	}
}
